package xcmg.syn.infra;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import yb.ecp.fast.infra.infra.ActionResult;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by john on 2017/4/11.
 */
public class BasicController {
    @Autowired
    private HttpServletRequest request;


    public <T> ActionResult<T> actionResult(ErrorCode code, T value) {
        return new ActionResult<T>(code.getCode(),
                code.getDesc(),
                value);
    }

    public <T> ActionResult<T> actionResult(T value) {
        ErrorCode code = ErrorCode.Success;
        return actionResult(code, value);
    }

    public String getUserId(HttpServletRequest request) {
        return request.getHeader("x-user-id");
    }


    public String getUserId() {
        if (null == request) {
            throw new RuntimeException("request is null");
        }
        String userId = request.getHeader("x-user-id");
        if (StringUtils.isNotBlank(userId)) {
            return userId;
        }
        throw new RuntimeException("获取小程序登陆信息失败");
    }

}
